﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Estimate.Common.Estimate;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Entity;
using Estimate.Common.DataFileFinder;
using Estimate.Reports;

namespace EstimateService
{
    /// <summary>
    /// ForeCompareReportHandler 的摘要说明
    /// </summary>
    public class ForeCompareReportHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string begindate = context.Request.QueryString["begindate"] != null ? context.Request.QueryString["begindate"].ToString() : "";
            string enddate = context.Request.QueryString["enddate"] != null ? context.Request.QueryString["enddate"].ToString() : "";
            string sendpoints = context.Request.QueryString["sendpoints"] != null ? context.Request.QueryString["sendpoints"].ToString() : "";
            string wthspans = context.Request.QueryString["wthspans"] != null ? context.Request.QueryString["wthspans"].ToString() : "";
            string tempspans = context.Request.QueryString["tempspans"] != null ? context.Request.QueryString["tempspans"].ToString() : "";
            string forecaster = context.Request.QueryString["forecaster"] != null ? context.Request.QueryString["membertable"].ToString() : "";
            string foreproducts = context.Request.QueryString["foreproducts"] != null ? context.Request.QueryString["foreproducts"].ToString() : "";
            string station = context.Request.QueryString["station"] != null ? context.Request.QueryString["station"].ToString() : "";
            string tid = context.Request.QueryString["tid"] != null ? context.Request.QueryString["tid"].ToString() : "";
            bool filterdate = context.Request.QueryString["filterdate"] != null ? true : false;

            //获取回调函数名
            string callback = context.Request.QueryString["callback"];
            //json数据
            string json = GetForeCompareReportData(begindate, enddate, sendpoints, wthspans, tempspans, forecaster, foreproducts, station, tid, filterdate);

            context.Response.ContentType = "application/json";
            //输出：回调函数名(json数据)
            context.Response.Write(callback + "(" + json + ")");
        }

        public bool IsReusable
        { 
            get
            {
                return false;
            }
        }

        public string GetForeCompareReportData(string begindate, string enddate, string sendpoints, string wthspans, string tempspans, string forecaster, string foreproducts, string station, string tid, bool filterdate=false)
        {
           
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];

            DateTime beginDate = DateTime.Parse(begindate);
            DateTime endDate = DateTime.Parse(enddate);
            ForeSemiType semiType = ForeSemiType.Auto;

            List<ForeSendpointType> sendpointList = new List<ForeSendpointType>();
            foreach (string s in sendpoints.Split(','))
                sendpointList.Add((ForeSendpointType)Enum.Parse(typeof(ForeSendpointType), s.Replace("'", "")));
            ForeSendpointType[] sendpointArray = sendpointList.ToArray();


            List<ForeSpanType> spanList = new List<ForeSpanType>();
            foreach (string s in wthspans.Split(','))
                spanList.Add((ForeSpanType)Enum.Parse(typeof(ForeSpanType), s.Replace("'", "")));
            ForeSpanType[] wthSpanArray = spanList.ToArray();

            spanList.Clear();
            foreach (string s in tempspans.Split(','))
                spanList.Add((ForeSpanType)Enum.Parse(typeof(ForeSpanType), s.Replace("'", "")));
            ForeSpanType[] tempSpanArray = spanList.ToArray();

            ForeSendpointType[] sendPoints = new ForeSendpointType[] { ForeSendpointType.H06, ForeSendpointType.H16 };

            List<string> foreProducts = new List<string>();

            foreach (string foreProduct in foreproducts.Split(','))
                foreProducts.Add(foreProduct);



            ForeCompareForeReport foreCompareForeReport = new ForeCompareForeReport(connectionString, forecaster, foreProducts, station, tid, false);

            foreCompareForeReport.PrepareData(beginDate, endDate, semiType, wthSpanArray, tempSpanArray, sendpointArray, true);

            return foreCompareForeReport.GetReportDataString(false);
        }

    }
}